<?php
namespace Home\Controller;
use Think\Controller;

class UserController extends  Controller{
    //验证码
    public function  verify(){
        $config=array(
            'fontSize' =>16,
            'imageW'=>110,
            'imageH'=>40,
            'length' =>4,
            'useCurve'=>false,
            'useNoise'=>false,
        );
        $vf=new \Think\Verify($config);
        $vf->entry();
    }
    //添加用户
    public function  register(){
   $model=D('User');
  if(IS_POST){
       if($data=$model->create()){
           $vf=new \Think\Verify();
           if(!$vf->check(I('POST.verify'))) $this->error('验证码错误');
           else {
               $data['password']=md5($data['password']);
               $model->add($data);
           }

           echo "用户注册成功";
           redirect('login',1,'注册成功');
       }else{
           dump($model->getError());
           redirect('register',1,'注册失败');
       }
   }
        $this->display();
    }

    //登录用户
    public function login()
    {
        $verify=new \Think\Verify();
        if(IS_POST){
            //判断验证码
          if(!$verify->check(I('post.verify'))) $this->error('验证码错误');
            $user=M('User')->where(array('username'=>I('post.user')))->find();
//            dump($user['username']);exit();
            if(!$user) $this->error('用户名不存在');
            //检验密码
            $password=I('post.password');
           if(md5($password)!=$user['password']) $this->error('用户名不存在');
          session('it_user',$user['user']);

        }
        $this->display();
    }
}